package com.xidian.zhiancloud.service.myyb;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SysJobMapper;
import com.xidian.zhiancloud.dao.SysJobTableMapper;
import com.xidian.zhiancloud.dao.SysOrganizationAbandonMapper;
import com.xidian.zhiancloud.dao.SysUserAbandonMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.SysJob;
import com.xidian.zhiancloud.model.SysJobTable;
import com.xidian.zhiancloud.model.SysOrganizationAbandon;
import com.xidian.zhiancloud.model.SysUserAbandon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 */
@Service
public class ISysUserService extends ServiceImpl<SysUserAbandonMapper, SysUserAbandon> implements IService<SysUserAbandon> {

    @Autowired
    private SysUserAbandonMapper userMapper;

    @Autowired
    private SysOrganizationAbandonMapper organizationMapper;

    @Autowired
    private SysJobMapper jobMapper;

    @Autowired
    private SysJobTableMapper jobTableMapper;

    public SysUserAbandon selectByPrimaryKey(Long id) {
        return userMapper.selectById(id);
    }

    public List<SysUserAbandon> selectUserList(Map mapResult) {
        return userMapper.selectUserList(mapResult);
    }

    public int selectByUserPosition(String position) {
        return userMapper.selectByUserPosition(position);
    }

    public int selectByUserPost(String post) {
        return userMapper.selectByUserPost(post);
    }

    public List<SysUserAbandon> selectByPhoneNum(String phoneNum) {
        return userMapper.selectByPhoneNum(phoneNum);
    }

    public SysUserAbandon selectByUserNameAndPassWords(String username, String passwords){
        return userMapper.selectByUserNameAndPassWords(username,passwords);
    }

    public List<SysUserAbandon> selectByCompanyAndRoleId(Long companyId, Long roleId){
        return userMapper.selectByCompanyAndRoleId(companyId, roleId);
    }

    public List<String> queryAllMenuId(String userId) {
        return userMapper.queryAllMenuId(userId);
    }

    public List<SysUserAbandon> queryList(Map<String, Object> map) {
        return userMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return userMapper.queryTotal(map);
    }

    public void patchById(SysUserAbandon user){
        int userRes = userMapper.updateById(user);
        if(userRes == 0){
            throw new CustomizeException(UserResponseEnums.USER_UPDATE_FAILED);
        }

        SysOrganizationAbandon organization = organizationMapper.selectById(user.getOrganizationId());
        int orgRes = organizationMapper.updateById(organization);
        if(orgRes == 0){
            throw new CustomizeException(UserResponseEnums.ORGANIZATION_UPDATE_FAILED);
        }

        SysJob job = jobMapper.selectById(user.getPositionId());
        int jobRes = jobMapper.updateById(job);
        if(jobRes == 0){
            throw new CustomizeException(UserResponseEnums.JOB_UPDATE_FAILED);
        }

        SysJobTable jobTable = jobTableMapper.selectById(user.getPostId());
        int jobTabRes = jobTableMapper.updateById(jobTable);
        if(jobTabRes == 0){
            throw new CustomizeException(UserResponseEnums.JOB_TABLE_UPDATE_FAILED);
        }

    }

    /**
     * 添加
     * @param user
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void add(SysUserAbandon user) {
        int result = userMapper.insert(user);

        SysOrganizationAbandon organization = new SysOrganizationAbandon();
        SysOrganizationAbandon organizations =organizationMapper.selectById(user.getOrganizationId());
        organization.setName(organizations.getName());
        organization.setCompanyId(user.getCompanyId());
        organization.setDepartId(organizations.getDepartId());
        organization.setDepartmentPeopleNums(organizations.getDepartmentPeopleNums()+1);
        organization.setPid(organizations.getPid());
        organization.setIsSafeDepartment(organizations.getIsSafeDepartment());
        organization.setCodes(organizations.getCodes());
        organization.setUserid(organizations.getUserid());
        organization.setOfficetel(user.getPhoneNum());
        organization.setPositionId(organizations.getPositionId());
        organization.setStatus(0);
        organization.setCreateTime(new Date());
        organization.setUpdateTime(new Date());
        int orgResult = organizationMapper.insert(organization);

        SysJob jobs = jobMapper.selectById(user.getPositionId());
        SysJob job = new SysJob();
        job.setCompanyId(user.getCompanyId());
        job.setPostOrder(jobs.getPostOrder());
        job.setPostName(jobs.getPostName());
        job.setPostNum(jobs.getPostNum()+1);
        job.setPositionNum(jobs.getPositionNum()+1);
        job.setStatus(0);
        job.setCreatorId(user.getId());
        job.setCreateTime(new Date());
        job.setUpdateTime(new Date());
        int jobResult =jobMapper.insert(job);

        SysJobTable jobTables= jobTableMapper.selectById(user.getPostId());
        SysJobTable jobTable = new SysJobTable();
        jobTable.setCompanyId(user.getCompanyId());
        jobTable.setOrganizationId(user.getOrganizationId());
        jobTable.setPositionId(user.getPositionId());
        jobTable.setPositionName(jobTables.getPositionName());
        jobTable.setDuty(jobTables.getDuty());
        jobTable.setAnnexUrl(jobTables.getAnnexUrl());
        jobTable.setStatus(0);
        jobTable.setCreateTime(new Date());
        jobTable.setUpdateTime(new Date());
        int jobTaResult = jobTableMapper.insert(jobTable);
        if (result == 0 && orgResult == 0 && jobResult == 0 && jobTaResult == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }

    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Long id) {
        SysUserAbandon user = userMapper.selectById(id);
        int useRes = userMapper.deleteById(id);
        int orgRes = organizationMapper.deleteById(user.getOrganizationId());
        int jobRes = jobMapper.deleteById(user.getPositionId());
        int jobTabRes = jobTableMapper.deleteById(user.getPostId());
        if (useRes == 0 && orgRes == 0 && jobRes == 0 && jobTabRes == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }

    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteByIds(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            SysUserAbandon user = userMapper.selectById((ids[i]));
            int result = userMapper.deleteById(ids[i]);
            int orgresult = organizationMapper.deleteById(user.getOrganizationId());
            int jobresult = jobMapper.deleteById(user.getPositionId());
            int jobTabresult = jobTableMapper.deleteById(user.getPostId());
            if (result == 0 && orgresult==0 && jobresult == 0 && jobTabresult==0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    }


}
